package com.jiu345.users.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jiu345.core.page.PageQuery;
import com.jiu345.core.page.TableDataInfo;
import com.jiu345.users.pojo.Address;
import com.jiu345.users.pojo.Region;
import com.jiu345.users.service.IAddressService;
import com.jiu345.users.service.IRegionService;
import com.jiu345.users.service.impl.IRegionServiceImpl;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * package:AddressController
 *
 * @author MrLei QQ:235921049
 * desc:用户地址
 * @Date 2024.07.28 21:34
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user/address")
public class AddressController extends BaseController {
    private final IAddressService addressService;
    private final IRegionService regionService;
    /*根据用户ID查询收货地址*/
    @GetMapping("/list")
    public TableDataInfo<Address> list(Address bo,PageQuery pageQuery){
        return addressService.queryPageList(bo,pageQuery);
    }
    /*根据主键查询*/
    @GetMapping("/{id}")
    public R<Address> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(addressService.queryById(id));
    }
    /*新增收货地址*/
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Address bo) {
        return toAjax(addressService.insertByBo(bo));
    }
    /*修改收货地址*/
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Address bo) {
        return toAjax(addressService.updateByBo(bo));
    }
    /*设置默认地址*/
    @PutMapping("/updateStatus/{id}")
    public R<Void> updateStatus(@NotNull(message = "主键不能为空")
                                @PathVariable Long id
    ) {
        return toAjax(addressService.updateStatus(id));
    }
    /*地区列表*/
    @GetMapping("/region")
    public R<List<Region>> list (Region bo ){
        return R.ok(regionService.selectList(bo));
    }


    /**
     * 删除地址
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(addressService.deleteWithValidByIds(List.of(ids), true));
    }
}
